package com.huanglj.himalaya.presenters;

import com.huanglj.himalaya.interfaces.IRecommendCallBack;
import com.huanglj.himalaya.interfaces.IRecommendPresenter;
import com.huanglj.himalaya.utils.LogUtil;
import com.huanglj.himalaya.views.UILoader;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.huanglj.himalaya.utils.Constants.RECOMMEND_LIKE_COUNT;

public class RecommendPresenter implements IRecommendPresenter {
    private static final String TAG = "RecommendPresenter";
    private List<Album> mAlbumList;
    private List<IRecommendCallBack> mCallBacks = new ArrayList<>();

    //设置成单例模式
    private RecommendPresenter(){}

    private static RecommendPresenter sInstance = null;

    /**
     * 懒汉式单例
     * @return
     */
    public static RecommendPresenter getInstance(){
        if (sInstance == null) {
            synchronized (RecommendPresenter.class){
                if (sInstance == null) {
                    sInstance = new RecommendPresenter();
                }
            }
        }
        return sInstance;
    }
    @Override
    public void getRecommendList() {
        unLoading();
        final Map<String, String> map = new HashMap<String, String>();
        map.put(DTransferConstants.LIKE_COUNT, RECOMMEND_LIKE_COUNT);
        CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                mAlbumList = gussLikeAlbumList.getAlbumList();
                //得到数据跟新UI
                handleRecommendResult(mAlbumList);
                //更新完成后取消刷新
                UILoader.mRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(int i, String s) {
                LogUtil.e(TAG,"Error code: " + i + ", ErrorMsg: " + s);
                handleNetworkErrorResult(i,s);
            }
        });
    }

    private void unLoading() {
        if (mCallBacks != null) {
            for (IRecommendCallBack callBack : mCallBacks) {
                callBack.onLoading();
            }
        }
    }

    private void handleNetworkErrorResult(int i, String s) {
        if(mCallBacks != null){
            for (IRecommendCallBack callBack : mCallBacks) {
                callBack.onNetworkError();
            }
        }
    }

    private void handleRecommendResult(List<Album> albumList) {
        //通知UI
        if(mCallBacks != null){
            for (IRecommendCallBack callBack : mCallBacks) {
                if (albumList.size() != 0) {
                    callBack.onRecommendListLoaded(albumList);
                }else {
                    callBack.onEmpty();
                }
            }
        }
    }

    @Override
    public void refreshMore() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void registerViewCallBack(IRecommendCallBack callBack) {
        if(mCallBacks != null && !mCallBacks.contains(callBack)){
            mCallBacks.add(callBack);
        }
    }

    @Override
    public void unRegisterViewCallBack(IRecommendCallBack callBack) {
        mCallBacks.remove(callBack);
    }
}
